package com.central.rearService.service.serviceImp

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.util.ObjectUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.central.common.entity.Attention
import com.central.common.enum.AttentionTypeEnum
import com.central.common.feign.UserService
import com.central.common.model.*
import com.central.common.service.impl.SuperServiceImpl
import com.central.rearService.mapper.AttentionMapper
import com.central.rearService.service.AttentionService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * @author  fly
 * @date  2021/5/26 14:53
 * @version 1.0
 */
@Service
open class AttentionServiceImp : SuperServiceImpl<AttentionMapper, Attention>(), AttentionService {

    @Autowired
    private lateinit var userService: UserService


    override fun getMyAttentionUserList(userId: Long): MutableList<UserInfo>? = list(KtQueryWrapper(Attention::class.java)
        .eq(Attention::createBy, userId)
        .eq(Attention::type, AttentionTypeEnum.USER.code)
        .orderByDesc(Attention::createTime)).takeIf { it.isNotEmpty() }?.run {
        mapNotNull {
            val userInfo = UserInfo()
            BeanUtil.copyProperties(userService.selectByUserId(it.attentionId), userInfo)
            userInfo
        }.toMutableList()
    }

    override fun booleanAttention(attentionId: Long, userId: Long, attentionTypeEnum: AttentionTypeEnum): RealDataResult? = RealDataResult()
        .apply { booleanAttention = ObjectUtil
            .isNotEmpty(getOne(KtQueryWrapper(Attention::class.java)
                .eq(Attention::attentionId, attentionId)
                .eq(Attention::type, attentionTypeEnum.code)
                .eq(Attention::createBy, userId))) }

    override fun resolveAttention(attentionId: Long, sysUser: SysUser, type: Int): CommonResult<Boolean> = success(
        getOne(KtQueryWrapper(Attention::class.java).eq(Attention::attentionId, attentionId)
            .eq(Attention::type, type)
            .eq(Attention::createBy, sysUser.id))?.run { removeById(id) }
            ?: save(Attention(attentionId = attentionId, type = type).apply { createBy = sysUser.id })
    , "操作成功")

    override fun getMyAttentionIdList(userId: Long, attentionTypeEnum: AttentionTypeEnum): List<String>? =
        list(KtQueryWrapper(Attention::class.java)
        .eq(Attention::createBy, userId).eq(Attention::type, attentionTypeEnum.code).orderByDesc(Attention::createTime))
        .takeIf { it.isNotEmpty() }?.run { mapNotNull { it.attentionId.toString() } }

    override fun getMyFansUserId(userId: Long): List<String>? = list(KtQueryWrapper(Attention::class.java)
        .eq(Attention::attentionId, userId)
        .eq(Attention::type, AttentionTypeEnum.USER.code)
        .orderByDesc(Attention::createTime))
        .takeIf { it.isNotEmpty() }?.run { mapNotNull { it.createBy.toString() } }
}